package info.wangyuan.agent.controller.portal;


import info.wangyuan.agent.common.Enum.MsgEnum;
import info.wangyuan.agent.entity.dto.RagStreamChatRequestDTO;
import info.wangyuan.agent.entity.vo.CurrentModelConfigVO;
import info.wangyuan.agent.service.conversation.ChatSessionManager;
import info.wangyuan.agent.service.conversation.ConversationDomainService;
import info.wangyuan.agent.service.curd.CommonService;
import info.wangyuan.agent.utils.Result;
import info.wangyuan.agent.utils.WordsFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * 对话控制器
 *
 * @author Albert
 * @since 2025-07-12 17:57:53
 */
@Slf4j
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/rag/search")
public class RagSearchController {

    private final CommonService commonService;
    private final ChatSessionManager chatSessionManager;
    private final ConversationDomainService conversationDomainService;

    public RagSearchController(CommonService commonService, ChatSessionManager chatSessionManager, ConversationDomainService conversationDomainService) {
        this.commonService = commonService;
        this.chatSessionManager = chatSessionManager;
        this.conversationDomainService = conversationDomainService;
    }

    /**
     * RAG流式问答
     */
//    @OpenIdRateLimit // 1分钟最多5次请求
    @GetMapping("/streamChat")
    public SseEmitter ragStreamChat(@RequestParam String sessionId, @RequestParam String message) {
        return conversationDomainService.ragStreamChat(
                RagStreamChatRequestDTO.builder()
                        .sessionId(sessionId)
                        .question(message)
                        .build()
        );
    }

    /**
     * RAG流式问答主动中断
     */
    @PostMapping("/interrupt/{sessionId}")
    public Result<String> ragStreamChatCancel(@PathVariable String sessionId) {
        log.info("用户请求中断会话: {}", sessionId);

        boolean success = chatSessionManager.interruptSession(sessionId);

        if (success) {
            log.info("成功中断会话: sessionId={}", sessionId);
            return Result.success("对话已中断");
        } else {
            log.warn("中断会话失败，会话不存在: sessionId={}", sessionId);
            return Result.error(MsgEnum.SSE_INTERRUPT_ERROR);
        }
    }

    /**
     * 获取当前使用的 LLM 模型信息
     */
    @GetMapping("/getCurrentModel")
    public Result<CurrentModelConfigVO> getCurrentModel() {
        return Result.success(commonService.getCurrentModelConfig());
    }

    /**
     * 获取当前使用的智能体欢迎词
     */
    @GetMapping("/welcomeMsg")
    public Result<String> getCurrentWelcomeMsg() {
        return Result.success(commonService.getCurrentWelcomeMsg());
    }

    /**
     * 热更新外置敏感词库
     */
    @GetMapping("/reloadWords")
    public Result<Void> reloadSensitiveWords() {
        try {
            WordsFilter.reload();
        } catch (Exception e) {
            log.error("敏感词库热更新失败", e);
            return Result.error(MsgEnum.SENSITIVE_WORDS_RELOAD_ERROR);
        }
        return Result.success();
    }

}
